<?php

/*
 * This file will perform the ajax for deleting a route.
 * This is being used by admRouteView.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: December 17, 2011
 * 
 * In parameters: routeId
 * Out parameters: flag and msg about the success of deleting the route
 */

//********************************************************************************************************
/*
 * Define the functions in here
 */

    // Start - Check if the route has been routed
    function notYetRouted($routeId){
        $query = 
        "
            SELECT *
            FROM

                -- Connect the routeId by using RouteStep
                -- Get rows that are not Archived
                (SELECT DocLastStep.logId as logId, DocLastStep.stepId as stepId, DocLastStep.documentId as documentId, DocLastStep.activity as activity, RS.routeId as routeId
                FROM

                    -- Get the last step of the main document
                    (SELECT *
                    FROM 

                        -- Get the needed information of main document from the log
                        (SELECT L.logId as logId, L.stepId as stepId, L.documentId as documentId, L.activity as activity
                        FROM Log as L, MainDocument as MD, DocumentSetup as DS
                        WHERE L.documentId = MD.mainDocumentId AND MD.documentSetupId = DS.documentSetupId AND DS.type = 'MainDocument'
                        ORDER BY L.logId DESC) as MainDocs

                    GROUP BY documentId) as DocLastStep, RouteStep as RS

                WHERE RS.stepId = DocLastStep.stepId) as OngoingRoutes

            WHERE routeId = '$routeId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if the route has been routed
    
    // Start - Update the routeLastUpdated of the $updateRouteId
    function lastUpdateRoute($updateRouteId) {
        // Get current date and time
        date_default_timezone_set('Asia/Singapore');
        $currentDateTime= date("Y-m-d H:i:s" , time());
        
        $query = 
        "
            UPDATE DocumentRoute
            SET routeLastUpdated = '$currentDateTime'
            WHERE routeId = '$updateRouteId'
        ";
    
        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Update the routeLastUpdated of the $updateRouteId
    
    // Start - Get the name of the route that will be deleted
    function getName($routeId) {
        $query =
            "
                SELECT *
                FROM DocumentRoute
                WHERE routeId = '$routeId' AND routeLastUpdated IS NULL
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        $row = mysql_fetch_array($result);
        
        return $row['routeName'];
    } // Start - Get the name of the route that will be deleted
    
//********************************************************************************************************

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post 
    $routeId = $_POST['routeId'];
    

    if ( notYetRouted($routeId) == true ) {
        $routeName = getName($routeId);
      //  lastUpdateRoute($routeId);
        
        $deleteQuery="DELETE FROM `greenroute`.`DocumentRoute` WHERE `DocumentRoute`.`routeId` = $routeId";
        mysql_query($deleteQuery) or die ('Error in query: $deleteQuery. ' . mysql_error());
        
        $jsondata = array();
        $jsondata['msg'] = $routeName." was successfully deleted.";
        $jsondata['flag'] = "success";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    else {
        $jsondata = array();
        $jsondata['msg'] = "Error : ".$routeName." was used already.";
        $jsondata['flag'] = "error";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }

    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
